
define([
        "jquery",
        "IO",
        "UPLOADUI",
        "UPLOAD",
        "jqueryUI",
        "bootbox",
        "test"
    ], function ($, IO, upui, up, jui, bootbox, definebt) {
        $(function () {
            $(".form_datetime1").datetimepicker({
                format: "yyyy/MM/dd",
                showMeridian: true,
                autoclose: true,
                todayBtn: true,
                minView:4,
            });
            $(".form_datetime2").datetimepicker({
                format: "yyyy/MM/dd",
                showMeridian: true,
                autoclose: true,
                todayBtn: true,
                minView:4,
            });

            var train_id; //培训id
            var train_start_time1; //培训开始时间
            var train_start_time2; //培训开始时间(修改)
            gettable();
            function  gettable(searchContent) {
                var grid;
                //每列数据列性
                var columns = [
                    {
                        title: '编号',
                        dataIndex: 'train_id',
                        dataSource: function (data, datarow, gridobj, current_column) {
                            return "<span>" + data[datarow].train_id + "</span>";
                        }
                    },
                    {
                        title: '名称',
                        dataIndex: 'train_name',
                        dataSource: function (data, datarow, gridobj, current_column) {
                            return "<span>" + data[datarow].train_name + "</span>";
                        }
                    },
                    {
                        title: '操作',
                        dataIndex: 'action',
                        dataSource: function (data, datarow, gridobj, current_column) {
                            return "<span><a href='#' class='editors' style='color: green;' train_name='"+data[datarow].train_name+"' train_id='"+ data[datarow].train_id+"' train_start_time='"+data[datarow].train_start_time+"'>编辑</a></span>&nbsp;&nbsp;&nbsp;<span><a href='#' class='delete' style='color: red;' train_id='" + data[datarow].train_id + "'>删除</a></span>&nbsp;&nbsp;&nbsp;<span><a download href='http://zjprod.snsunion.cn:8081/CorporateTraining/api/v1/trains/export?train_id="+data[datarow].train_id+"'  style='color: green;' class='export'>导出答案</a></span>&nbsp;&nbsp;&nbsp;<span><a href='#' class='into' style='color: green;' train_name='" + data[datarow].train_name + "' train_id='" + data[datarow].train_id + "'>进入题库</a></span>";
                        }
                    },

                ];

                //数组初始化

                function dataInit() {
                    grid = new Grid("/trainsList", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(searchContent), 10);
                    //初始化
                    grid.Init();
                }

                //获取输入框内容
                function get_input_param(searchContent) {

                    return "listType=0";
                }

                dataInit();
            }

            //点击新增咨询按钮
            $('#btnAddTrain').on('click',function () {
                $('.form_datetime1').val('');
                $('#trainName').val('');
                $('#myModal1').modal("show");
            })
            //点击新增咨询确定按钮
            $('#btnAddTrainSure').on('click',function () {
                var  trainName=$('#trainName').val();// 培训名称
                if(trainName===""){
                    bootbox.alert("培训名不能为空!");
                    return;
                }
                if(train_start_time1===""){
                    bootbox.alert("培训开始时间不能为空!");
                    return;
                }
                var  pamars={};
                pamars.train_name=trainName;//培训名称
                pamars.train_start_time=train_start_time1;//培训开始时间
                IO.post("/trainsAdd",pamars,function(d){
                    if(d.code=="0"){
                        bootbox.alert("新增成功");
                        $('#myModal1').modal('hide');
                        gettable();
                    }else{
                        bootbox.alert("新增失败");
                    }
                });
            })

            //列表点击删除按钮
            $(document).on('click','.delete',function () {
                train_id=$(this).attr("train_id");//培训id
                if(train_id){
                    bootbox.confirm("确定要删除该培训?",function(result){
                        if(result) {
                            IO.get("/train_delete",{train_id:train_id},function(d){
                                if(d.code=='0'){
                                    bootbox.alert(d.msg,function(){
                                        gettable();
                                    })}
                            });
                        }
                    });
                }
            })

            //列表进入题库
            $(document).on('click','.into',function (){
                var train_id=$(this).attr("train_id");//培训id
                var train_name=$(this).attr("train_name");//培训名称
                if(train_id){
                    location.href = '/trainQuestions?train_id='+train_id+"&train_name="+train_name;
                }
            })
            //模态框隐藏1
            $('#myModal1').on('hide.bs.modal', function () {
                // $('#trainName').val('');
            })
            //模态框隐藏2
            $('#myModal2').on('hide.bs.modal', function () {
                // $('#trainNameUpdate').val('');
            })

            //点击编辑按钮
            $(document).on('click','.editors',function () {
                train_id=$(this).attr("train_id");//培训id
                var train_name=$(this).attr("train_name");//培训名称
                train_start_time2=$(this).attr("train_start_time");//培训开始时间
                if(train_id){
                    $('#trainNameUpdate').val(train_name);
                    $('.form_datetime2').val(new Date(parseInt(train_start_time2) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ").substring(0,9));
                    $('#myModal2').modal("show");

                }
            })

            //编辑修改按钮
            $("#btnUpdateTrainSure").on('click',function (e) {

                if(train_id){
                    var train_name=$('#trainNameUpdate').val();
                    if(train_name===""){
                        bootbox.alert("培训名不能为空!");
                        return;
                    }
                    var param={}
                    param.train_id=train_id;
                    param.train_name=train_name;
                    param.train_start_time=train_start_time2;
                    IO.get("/train_update",param,function(d){
                        if (d.code == 0) {
                            $('#myModal2').modal("hide");
                            bootbox.alert(d.msg,function(){
                                gettable();
                            })}
                        else{
                            bootbox.alert(d.msg,function(){

                            })}


                    });
                }
            })


            //点击日历控件
            $('.form_datetime1')
                .datetimepicker()
                .on('changeDate', function(ev){
                    var test = (ev.date).toString();
                    var d=new Date(test);
                    var date=new Date();
                    date.setFullYear(d.getUTCFullYear());
                    date.setMonth(d.getUTCMonth());
                    date.setDate(d.getUTCDate());
                    date.setHours(d.getUTCHours());
                    date.setMinutes(d.getUTCMinutes());
                    date.setSeconds(d.getUTCSeconds());
                    train_start_time1=Date.parse(date)/1000;

                });
            $('.form_datetime2')
                .datetimepicker()
                .on('changeDate', function(ev){
                    var test = (ev.date).toString();
                    var d=new Date(test);
                    var date=new Date();
                    date.setFullYear(d.getUTCFullYear());
                    date.setMonth(d.getUTCMonth());
                    date.setDate(d.getUTCDate());
                    date.setHours(d.getUTCHours());
                    date.setMinutes(d.getUTCMinutes());
                    date.setSeconds(d.getUTCSeconds());
                    train_start_time2=Date.parse(date)/1000;
                });



        })
    }

)
